package com.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.beans.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.beans.Chapter;
import com.service.impl.ChapterServiceImpl;


@Controller
@RequestMapping("/chapter")
public class ChapterController {
    @Resource
    ChapterServiceImpl service;

    @RequestMapping("/getchapter")
    public String get(String novelname, ModelMap model) {
        System.out.println("get==>" + novelname);
        if (novelname != null && !novelname.equals("")) {
            List<Chapter> list = service.getChapter(novelname);
            model.addAttribute("list", list);
            return "chapter/allchapter";
        }
        model.addAttribute("error", "未查询到响应的数据，请您检查输入的数据名称是否正确~");
        System.err.println("error");
        return "chapter/allchapter";
    }




    @GetMapping("/updatechapter")
    public String updateChapter(int id, ModelMap m) {
        Chapter chapter = service.getChapterById(id);
        m.put("chapter", chapter);
        return "chapter/updatechapter";
    }

    @RequestMapping("/getchapterlist")
    public String getlist(String novelname, ModelMap m) {
        List<Chapter> list = service.getChapter(novelname);
        m.put("list", list);
        return "chapter/chapterlist";
    }


    /***************** 【以下为修改部分内容】 *********************/

    @RequestMapping("/getallchapter")
    public String getallChapter() {
        return "chapter/allchapter";
    }

    @RequestMapping("/queryByPage")
    @ResponseBody
    public Map<String, Object> queryByPage(Chapter chapter, Integer page, Integer limit) {
        return service.queryByPage(chapter, page, limit);
    }

    @PostMapping("/gotoupdatechapter")
    @ResponseBody
    public Integer updateu(@RequestBody Chapter chapter) {
        return service.updateChapter(chapter);
    }

    @RequestMapping("/deletechapter")
    @ResponseBody
    public Integer deleteu(int id) {
        return service.deleteChapter(id);
    }


    @RequestMapping("/addchapter")
    @ResponseBody
    public Integer addChapter(@RequestBody  Chapter chapter) {
        return service.addChapter(chapter);
    }
}
